package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.entity.xiangxian.Fengcai;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hongjianbin
 * @date 2020/7/20
 */
@RequestMapping("/fengcai")
@RestController
public class FengcaiController extends BaseController {
    /**
     * 新增风采
     * @param fengcai
     * @return
     */
    @PostMapping("/addfengcai")
    public Return addfengcai(Fengcai fengcai) {
        if(fengcai==null){
            return  Return.getErrorResult("参数为空");
        }
        if (fengcai.getContent().trim().length() > 200) {
            return Return.getErrorResult("风采内容过长");
        }
        if (fengcai.getInfo().trim().length() > 200) {
            return Return.getErrorResult("简介过长");
        }
        if (fengcai.getName().length() > 20) {
            return Return.getErrorResult("风采名称过长");
        }
        if (fengcai.getName()!=null) {
            long l=fengcaiService.findFengCaiByName(fengcai.getName());
            if(l>0){
                return Return.getErrorResult("风采名称已存在!");
            }
        }
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
       fengcai.setFcid(String.valueOf(snowFlake.nextId()));

        long l = fengcaiService.addfengcai(fengcai);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 风采详情
     * @param fcid
     * @return
     */
    @PostMapping("/fengcaiDetail")
    public Return getdetail(String fcid ){
        if(fcid==null){
            return  Return.getErrorResult("缺少id参数!");
        }
        Fengcai fengcai = fengcaiService.getdetail(fcid);
        return Return.getSuccessResult(fengcai);
    }

    /**
     *
     * @param type 风采类型
     * @param page  页码
     * @param size  每页个数
     * @return
     */
    @PostMapping("/fengcaiList")
    public Return epList(Integer type,Integer page,Integer size ){
        page = page == null ? 0 : page - 1;
        Page<Fengcai> list = fengcaiService.fengcaiList(type, page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list.getContent());
        map.put("total",list.getTotalElements());
        map.put("totalpage",list.getTotalPages());
        return Return.getSuccessResult(map);
    }

}
